package com.example.antivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.Model.music.MusicComment;
import com.example.Model.wen.WenContent;
import com.example.Model.wen.WenRelated;
import com.example.UI.Mylistview;
import com.example.adapter.MusicComAdapter;
import com.example.adapter.WenRelAdapter;

import com.example.retrofitinterface.music.MusicComInter;
import com.example.retrofitinterface.wen.WenInter;
import com.example.retrofitinterface.wen.WenRelInter;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebViewClient;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import sdstcom.lbq.one.R;
import sdstcom.lbq.one.lbq.Databeans.DbBean;
import sdstcom.lbq.one.lbq.LBQapplication;

public class WenActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    private WenRelated wenrelated;
    //private String url = "http://v3.wufazhuce.com:8000/api/question/1596?channel=mi&source=summary&source_id=9166&version=4.0.3&uuid=00000000-6990-fc62-ffff-ffffffbcadc0&platform=android";
    private String url1 = "http://v3.wufazhuce.com:8000/api/question/";
    private String url2;
    private String url3 = "?channel=mi&source=summary&source_id=9166&version=4.0.3&uuid=00000000-6990-fc62-ffff-ffffffbcadc0&platform=android";
    private String url ;
    //private String relatedurl = "http://v3.wufazhuce.com:8000/api/related/question/1596?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String relaterurl1="http://v3.wufazhuce.com:8000/api/related/question/";
    private String relaterurl;
    private String relaterurl3="?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";

    //private String commenturl = "http://v3.wufazhuce.com:8000/api/comment/praiseandtime/question/1596/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String commenturl1="http://v3.wufazhuce.com:8000/api/comment/praiseandtime/question/";
    private String commenturl;
    private String commenturl3="/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";

    private Toolbar tb;
    private TextView title;
    private TextView wener;
    private TextView question;
    private com.tencent.smtt.sdk.WebView wv;
    private TextView answerer;
    private Mylistview relatedlv;
    private Mylistview commentlv;
    private WenRelAdapter relatedadapter;
    private ArrayList<WenRelated.DataBean> relatedlist;
    private ArrayList<MusicComment.DataBeanX.DataBean> commentlist;
    private MusicComAdapter commentadapter;


    private String Dbtitle;
    private DbManager dbManager;
    private boolean ifpress = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_wen);
        tb = (Toolbar) findViewById(R.id.wen_tb);
        tb.setTitle("");
        setSupportActionBar(tb);
        tb.setNavigationIcon(R.mipmap.return_image_4_0);
        //返回监听
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        init();
    }

    private void init() {
        url2 = getIntent().getStringExtra("itemid");
        url = url1+url2+url3;
        relaterurl = relaterurl1+url2+relaterurl3;
        commenturl = commenturl1+url2+commenturl3;
        title = (TextView) findViewById(R.id.wen_title_tv);
        wener = (TextView) findViewById(R.id.wener_author_tv);
        question = (TextView) findViewById(R.id.question_tv);
        answerer = (TextView) findViewById(R.id.ansener_author_tv);
        wv = (com.tencent.smtt.sdk.WebView) findViewById(R.id.wen_wb);
        initwv();
        relatedlv = (Mylistview) findViewById(R.id.wen_related_lv);
        relatedlist = new ArrayList<>();
        relatedadapter = new WenRelAdapter(relatedlist,this);
        relatedlv.setAdapter(relatedadapter);
        relatedlv.setOnItemClickListener(this);

        commentlv = (Mylistview) findViewById(R.id.wen_comment_lv);
        commentlist = new ArrayList<>();
        commentadapter = new MusicComAdapter(commentlist,this);
        commentlv.setAdapter(commentadapter);
        loadwen();
        loadrelated();
        loadcomment();
        dbManager= x.getDb(((LBQapplication)getApplication()).config);
    }
    private void initwv() {
//        Log.e("PATH",getIntent().getStringExtra("path"));

        //wv.loadUrl(getIntent().getStringExtra("path"));
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);

        // 设置加载进来的页面自适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(true);

        settings.setTextSize(WebSettings.TextSize.LARGEST);

        wv.setWebViewClient(new WebViewClient(){

            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }
        });
    }

    //问题下载
    public void loadwen() {
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(WenInter.class).getcontent(url).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
               subscribe(new Action1<WenContent>() {
                   @Override
                   public void call(WenContent wenContent) {
                       Dbtitle=wenContent.getData().getQuestion_title();
                       title.setText(wenContent.getData().getQuestion_title());
                       wener.setText(wenContent.getData().getAsker().getUser_name());
                       answerer.setText(wenContent.getData().getAnswerer().getUser_name());
                       question.setText(wenContent.getData().getQuestion_content());
                       wv.loadData(wenContent.getData().getAnswer_content(),"text/html; charset=UTF-8",null);

                   }
               });

    }
    //问题相关下载
    public void loadrelated() {
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(WenRelInter.class).getrelated(relaterurl).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
              subscribe(new Action1<WenRelated>() {
                  @Override
                  public void call(WenRelated wenRelated) {
                      if(wenRelated!=null){
                          wenrelated = wenRelated;
                          relatedlist.addAll(wenRelated.getData());
                          relatedadapter.notifyDataSetChanged();
                      }

                  }
              });

    }
    public void loadcomment() {
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(MusicComInter.class).getcomment(commenturl).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<MusicComment>() {
                    @Override
                    public void call(MusicComment musicComment) {
                        commentlist.addAll(musicComment.getData().getData());
                        commentadapter.notifyDataSetChanged();
                    }
                });

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(this,WenActivity.class).putExtra("itemid",wenrelated.getData().get(position).getQuestion_id()));
    }
    public void collection(View view) throws DbException {
        if (ifpress == false) {
            view.setPressed(true);
            DbBean dbBean = new DbBean();
            dbBean.setItemid(url2);
            dbBean.setType("wenda");
            dbBean.setTitle(Dbtitle);
            dbBean.setSubtype("wenda");
            DbBean bean=dbManager.findById(DbBean.class,url2);
//            Log.e("TS", "collection: "+bean);
//            String sql="SELECT * FROM myonetable WHERE itemid="+url2;
            if (bean==null)
            {
                dbManager.save(dbBean);
                Toast.makeText(this,"收藏完成",Toast.LENGTH_SHORT).show();}
            ifpress=true;
        }else if (ifpress==true)
        {
            view.setPressed(false);
            dbManager.deleteById(DbBean.class,url2);
            ifpress=false;
            Toast.makeText(this,"取消收藏",Toast.LENGTH_SHORT).show();
        }
    }
}
